﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FollowPanelData
{
    public MiniUserInfo[] fans;
    public MiniUserInfo[] myFollows;
}
public class FollowPanel : AppUI
{
    List<FollowItem> m_FansItems = new List<FollowItem>();
    public Transform fansItemsParent;

    List<FollowItem> m_MyFollowItems = new List<FollowItem>();
    public Transform myFollowItemsParent;

    public FollowItem followItemPref;

    public GameObject fansItemsObj;
    public GameObject myFollowItemsObj;

    FollowPanelData m_Data;

    public static int lastSelectToggle = Toggle_Fans;

    public const int Toggle_Fans = 1;
    public const int Toggle_MyFollow = 2;

    public Text fansTitle;
    public Text myFollowTitle;
    public ListTogglesGroup togglesGroup;

    public override void Start()
    {
        if (togglesGroup)
            togglesGroup.Init(OnToggleSelect, lastSelectToggle);

        followItemPref.gameObject.SetActive(false);
    }

    public void Toggle(int id)
    {
        lastSelectToggle = id;

        if (togglesGroup)
            togglesGroup.Init(OnToggleSelect, lastSelectToggle);
    }

    void OnToggleSelect(int id)
    {
        lastSelectToggle = id;

        if (id == Toggle_Fans)
        {
            fansItemsObj.gameObject.SetActive(true);
            myFollowItemsObj.gameObject.SetActive(false);
            StartCoroutine(UnityFunctions.AdjustItemsLayout(fansItemsObj.transform));
        }
        else if (id == Toggle_MyFollow)
        {
            fansItemsObj.gameObject.SetActive(false);
            myFollowItemsObj.gameObject.SetActive(true);
            StartCoroutine(UnityFunctions.AdjustItemsLayout(myFollowItemsObj.transform));
        }
    }

    public void Init(FollowPanelData data)
    {
        m_Data = data;

        this.fansTitle.text = "粉丝 "+data.fans.Length;
        this.myFollowTitle.text = "关注 " + data.myFollows.Length;

        DateTime today = UnityFunctions.GetTimeZoneNowDateTime();

        UnityFunctions.ClearList(m_FansItems);

        for (int i = 0; i < data.fans.Length; i++)
        {
            var visitor = data.fans[i];
            var newItem = Instantiate(followItemPref, fansItemsParent);
            newItem.gameObject.SetActive(true);
            newItem.Init(visitor, OnSelectItem);

            m_FansItems.Add(newItem);
        }

        UnityFunctions.ClearList(m_MyFollowItems);

        for (int i = 0; i < data.myFollows.Length; i++)
        {
            var visitor = data.myFollows[i];
            var newItem = Instantiate(followItemPref, myFollowItemsParent);
            newItem.gameObject.SetActive(true);
            newItem.Init(visitor, OnSelectItem);

            m_MyFollowItems.Add(newItem);
        }

    }

    public void OnSelectItem(FollowItem item)
    {
        var userHome = this.GetComponentInParent<UIController>().ShowUI<UserHomePanel>();

        RequestServerController.instance.RequestUserMomentInfo(item.data.userId, (userInfo) =>
        {
            userHome.Init(userInfo);
        });
    }

    //public override void GoBack()
    //{
    //    UIController.ShowUI<MainPanel>();
    //}

    // Update is called once per frame
    void Update()
    {
        
    }
}
